<?php

class Lib_Logger {
	
	static private function get_log_dir(){
		global $CFG;
		return $CFG['log_path'].'/';
	}
	
	static private function write_log($log, $file){
		if (!is_file($file)) {
			$result = @touch($file);
			if (!$result) {
				return false;
			}
		}
		if (!is_writeable($file)) {
			$result = @chmod($file,777);
			if (!$result) {
				return false;
			}
		}
		return @file_put_contents($file, $log, FILE_APPEND)>0 ? true : false;
	}
	
	//数据库 记录的 log
	static public function log_db($info){
		$now_time = self::get_now_time();
		$string = sprintf( "%s\t%s\n", $now_time, $info);
		$log_date_str = self::get_now_time( true );
		$file_name = self::get_log_dir() . 'db.log.' . $log_date_str;
		return self::write_log($string,$file_name);
	}
	/**
	 * 记录API请求问题 
	 * 重点对空结果, 超时，302跳转，500错误等进行记录
	 * 
	 * @return void
	 */
	static public function log_api( $api_name, $error_info='' ){
		$now_time = self::get_now_time();
		$string = sprintf( "%s\t%s\t%s\n", $now_time, $api_name, $error_info );
		$log_date_str = self::get_now_time( true );
		$file_name = self::get_log_dir() . 'api.log.' . $log_date_str;
		return self::write_log($string,$file_name);
	}
	/**
	 * 记录对客户端的parse_error
	 *
	 * @param unknown_type $user_client
	 * @param unknown_type $client_version
	 * @param unknown_type $os_version
	 * @param unknown_type $url
	 * @return unknown
	 */
	static public function log_parse_error($user_client,$client_version,$udid,$os_version,$url){
		$error_info = sprintf("[%s] [%s] [%s] [%s] [%s] \n",$user_client,$client_version,$udid,$os_version,$url);
		$now_time = self::get_now_time();
		$string = sprintf( "%s\t%s\n", $now_time, $error_info );
		$log_date_str = self::get_now_time( true );
		$file_name = self::get_log_dir() . 'parse.log.' . $log_date_str;
		return self::write_log($string,$file_name);
	}
	/**
	 * 记录对服务端的error
	 *
	 * @param unknown_type $user_client
	 * @param unknown_type $client_version
	 * @param unknown_type $os_version
	 * @param unknown_type $url
	 * @return unknown
	 */
	static public function log_error($info){
		$now_time = self::get_now_time();
		$string = sprintf( "%s\t%s\n", $now_time, $info);
		$log_date_str = self::get_now_time( true );
		$file_name = self::get_log_dir() . 'error.log.' . $log_date_str;
		return self::write_log($string,$file_name);
	}
	/**
	 * 记录API请求问题 
	 * 重点对空结果, 超时，302跳转，500错误等进行记录
	 * 
	 * @return void
	 */
	static public function log_access(){
		global $user_info,$clean_gp;
		$time = date( 'Y-m-d H:i:s' );
		$url = get_page_url();
		$permanent_id = $user_info['permanent_id'];
		if(!isset ($permanent_id)) $permanent_id = '0';
		$cust_id = $user_info['cust_id'];
		$email = $user_info['email'];
		$user_client = $user_info['user_client'];
		$client_version = $user_info['client_version'];
		$ip = get_real_ip();
		$refer_url = isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'';
		$ua = $_SERVER['HTTP_USER_AGENT'];
		$timediff = time().','.$clean_gp['timestamp'].','.(time()-$clean_gp['timestamp']);
		$log = sprintf("[%s] [%s] [%s] [%s] [%s] [%s] [%s] [%s] [%s] [%s] [%s]\n",$time,$url,$permanent_id,$cust_id,$email,$ip,$refer_url,$ua,$user_client,$client_version,$timediff);
		$now_time = self::get_now_time();
		$string = sprintf( "%s\t%s\n", $now_time, $log);
		$log_date_str = self::get_now_time( true );
		$file_name = self::get_log_dir() . 'access.log.' . $log_date_str;
		return self::write_log($string,$file_name);
	}
	/**
	 * 记录string
	 * 重点对空结果, 超时，302跳转，500错误等进行记录
	 * 
	 * @return void
	 */
	static public function log_string($string){
		return false;
		$log_date_str = self::get_now_time( true );
		$file_name = self::get_log_dir() . 'access.log.' . $log_date_str;
		return self::write_log($string."\n",$file_name);
	}
	static private function get_now_time( $short = false ){
		if( true == $short ){
			return date( 'Ymd' );
		}else{
			return date( 'Y-m-d H:i:s' );
		}
	}
}
